// components/popup/popup.js
import user from '../../utils/user.js'
import { encryptedPhoneData} from '../../api/app.js'
const app = getApp()
Component({
    /**
   * 组件的属性列表
   */
    properties: {
        type: String,
        content: String
    },

    /**
   * 组件的初始数据
   */
    data: {

    },

    /**
   * 组件的方法列表
   */
    methods: {
        getUserProfile(){
            wx.getUserProfile({
                lang: 'zh_CN',
                desc: '用于展示头像信息',
                success:res=> {
                    user.avatarUrl = res.userInfo.avatarUrl
                    app.toast('授权获取成功')
                    this.triggerEvent('complete')
                },
                fail(err) {
                    console.log(err)
                }
            })
        },
        getPhoneNumber(res){
            if(!user.session_key){
                app.alert('操作过于频繁，请重新打开小程序').then(()=>{
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                })
                return
            }
            console.log(user)
            const data = {
                appId: app.globalData.appId,
                encryptedData: res.detail.encryptedData,
                sessionKey: user.session_key,
                iv: res.detail.iv
            }
            encryptedPhoneData(data).then(res=>{
                user.phoneNum = res.phoneNumber
                app.toast('授权成功')
                this.triggerEvent('complete',res.phoneNumber)
            })
        },
    }
})
